package com.sws.worktive.sign_up_service;

import junit.framework.Assert;
import android.content.Context;
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import com.sws.worktive.R;
import com.sws.worktive.dialogfragment.BaseDialogFragment;

public class CustomVerificationCodeDialog extends BaseDialogFragment implements OnClickListener{
	private OnEnterListener mOnEnterListener;
	
	public EditText editText;
	
	//=====================API=================================================
	public static CustomVerificationCodeDialog newInstance(
			FragmentManager fragmentManager, 
			String fragmentTag, 
			OnEnterListener onEnterListener){
		return newInstance(fragmentManager, fragmentTag, true, true, onEnterListener, null);
	}
	
	public static CustomVerificationCodeDialog newInstance(
			FragmentManager fragmentManager, 
			String fragmentTag, 
			boolean cancelTouchOutside, 
			boolean cancelable, 
			OnEnterListener onEnterListener, 
			OnDismissListener onDismissListener){
		
		Assert.assertNotNull("This tag is used to prevent double dialog creation. If you don't need a fragmentTag, please just provide a local one", fragmentTag);
		
		CustomVerificationCodeDialog customThreeButtonDialogFragment = (CustomVerificationCodeDialog) fragmentManager.findFragmentByTag(fragmentTag);
		if (customThreeButtonDialogFragment == null) {
			customThreeButtonDialogFragment = new CustomVerificationCodeDialog();
			
			customThreeButtonDialogFragment.setOnDismissListener(onDismissListener);
			customThreeButtonDialogFragment.setCancelTouchOutside(cancelTouchOutside);
			customThreeButtonDialogFragment.setCancelable(cancelable);
			customThreeButtonDialogFragment.setOnEnterListener(onEnterListener);
			return customThreeButtonDialogFragment;
		}
		return null;
	}
	
	public void setOnEnterListener(OnEnterListener onEnterListener){
		mOnEnterListener = onEnterListener;
	}
	
	//=======================Life Cycle==========================================
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		
        ViewGroup root = (ViewGroup)inflater.inflate(R.layout.dialog_fragment_verification_enter, null, false);
        Button enterButton = (Button)root.findViewById(R.id.btn_Enter);
        editText = (EditText) root.findViewById(R.id.edt_verification_code);
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
        enterButton.setOnClickListener(this);
        
        return root;
    }

	@Override
	public void onClick(View v) {
		unRegisterOndismissListener();
		int id = v.getId();
		if (id == R.id.btn_Enter) {
			if (mOnEnterListener != null) mOnEnterListener.onEnter(this);
		}
	}
	
	public interface OnEnterListener{
		void onEnter(CustomVerificationCodeDialog verificationCodeDialog);
	}
	
}
